﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;

namespace ERF
{
    /// <summary>
    /// This class is necessary to handle buttons in a clean way.
    /// This could be removed and replaced by a switch in the GetButtonState method if performance
    /// demands it, But it will be harder adding buttons if you do it in that manner.
    /// </summary>
    public class AssignedButton
    {
        /// <summary>
            /// They apparently decided to handle every single control device in a different way, 
            /// so we need to do this enum ourselves.
            /// </summary>
            public enum MouseButtons
            {
                LEFTBUTTON,
                RIGHTBUTTON,
            }
            
            ERFInputHandler handler;
            private Keys? key;
            private Buttons? button;
            private MouseButtons? mouseButton;
            /// <summary>
            /// Create a ButtonWrapper object for a key
            /// </summary>
            /// <param name="handler"></param>
            /// <param name="key"></param>
            public AssignedButton(ERFInputHandler handler, Keys key)
            {
                this.handler = handler;
                this.key = key;
            }

            /// <summary>
            /// Create a ButtonWrapper object for a button
            /// </summary>
            /// <param name="handler"></param>
            /// <param name="button"></param>
            public AssignedButton(ERFInputHandler handler, Buttons button)
            {
                this.handler = handler;
                this.button = button;
            }

            public AssignedButton(ERFInputHandler handler, MouseButtons mouseButton)
            {
                this.handler = handler;
                this.mouseButton = mouseButton;
            }

            /// <summary>
            /// Check if a button was pressed when the input class was updated last.
            /// </summary>
            /// <param name="button"></param>
            /// <returns></returns>
            public bool IsButtonDown()
            {
#if XBOX
                return handler.NewState.IsButtonDown((Buttons)button);
#else
                if (key != null)
                    return handler.NewState.IsKeyDown((Keys)key);

                if ((MouseButtons)mouseButton == MouseButtons.LEFTBUTTON)
                    return (handler.NewMouseState.LeftButton == ButtonState.Pressed);
                
                if((MouseButtons)mouseButton == MouseButtons.RIGHTBUTTON)
                    return (handler.NewMouseState.RightButton == ButtonState.Pressed);            
                
                return false;
#endif


            }


            /// <summary>
            /// Check if a button was released since the last update
            /// </summary>
            /// <returns></returns>
            public bool IsButtonReleased()
            {
#if XBOX
               return handler.NewState.IsButtonDown((Buttons)button);
#else
                if (key != null)
                    return handler.OldState.IsKeyDown((Keys)key);

                if(mouseButton == MouseButtons.LEFTBUTTON) 
                    return (handler.OldMouseState.LeftButton == ButtonState.Pressed);
                
                if(mouseButton == MouseButtons.RIGHTBUTTON)
                    return (handler.OldMouseState.RightButton == ButtonState.Pressed);            

                return false;
#endif


            }

    }
}
